namespace Eco.Contrib.Demo
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;
    using Eco.Handles;
    using Eco.ObjectRepresentation;
    using Eco.Services;
    using Eco.Windows.Forms;
    using Eco.WinForm;
    using Eco.Contrib.Demo.Model;
    using Eco.Contrib.Demo.EcoSpace;

    /// <summary>
    /// Summary description for WinScript.
    /// </summary>
    public partial class WinScript : System.Windows.Forms.Form
    {
        public WinScript(Eco.Contrib.Demo.EcoSpace.EcoContribDemoEcoSpace ecoSpace, CurrencyManagerHandle cmhRoot)
        {
            // Required for Windows Form Designer support
            this.InitializeComponent();

            // Set EcoSpace (actually stores it in rhRoot.EcoSpace)
            // Note that you need to set the EcoSpace property for 
            // each ReferenceHandle and VariableHandle you add to 
            // the form.
            this.rhRoot.EcoSpace = ecoSpace;
            this.rhRoot.SetElement(cmhRoot.Element.AsObject);
            EcoSpace.Undo.StartUndoBlock();
        }

        public EcoContribDemoEcoSpace EcoSpace
        {
            get
            {
                return (EcoContribDemoEcoSpace)this.rhRoot.EcoSpace;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCompile_Click(object sender, EventArgs e)
        {
            try
            {
                IEcoScriptLanguageService ESL = EcoSpace.GetEcoService<IEcoScriptLanguageService>();
                var script = rhRoot.Element.AsObject as Script;
                ESL.RegisterScript(script.Name, script.Program, script.Parameter);
            }
            catch (Exception eEx)
            {
                if (eEx.InnerException is EslException)
                {
                    rtbProgram.Select((eEx.InnerException as EslException).Pos, 2);
                }
                MessageBox.Show(eEx.Message);
                return;
            }
            MessageBox.Show("Program is OK.");
        }
    }
}
